package com.tf_cut.admin.util;

import com.tf_cut.admin.common.pojo.ApiResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ValidParamExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ApiResult<String> notValidException(MethodArgumentNotValidException ex){
        BindingResult bindingResult = ex.getBindingResult();
        String errorMsg = "";
        if(!bindingResult.getFieldErrors().isEmpty()){
            errorMsg = bindingResult.getFieldErrors().get(0).getDefaultMessage();
        }
        return ApiResult.of(null,1100,errorMsg);
    }
}
